//
//  WDWorkOrderSearchTopView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/9.
//

#import "WDWorkOrderSearchTopView.h"

@interface WDWorkOrderSearchTopView () <UITextFieldDelegate>

@property (nonatomic, strong) UIButton *btnSearch;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIImageView *searchImgView;

@property (nonatomic, strong) UITextField *searchTextField;

@end

@implementation WDWorkOrderSearchTopView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self initWithBtnSearch];
    
    [self initWithBgView];
    
    [self initWithSearchImgView];
    
    [self initWithSearchTextField];
}


- (void)initWithBtnSearch {
    if (!_btnSearch) {
        CGRect frame = CGRectMake(self.width - 66 - 20, (self.height - 34) * 0.5, 66, 34);
        _btnSearch = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] target:self action:@selector(actionWithSearch:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"搜索" forState:UIControlStateNormal];
            [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
            button.backgroundColor = kThemeColor;
            button.layer.cornerRadius = 6;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(20, (self.height - 34) * 0.5, self.btnSearch.left - 20 - 10, 34);
        _bgView = [[UIView alloc] initWithFrame:frame];
        _bgView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
        _bgView.layer.cornerRadius = 6;
        _bgView.layer.masksToBounds = YES;
        [self addSubview:_bgView];
    }
}

- (void)initWithSearchImgView {
    if (!_searchImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_order_search"];
        CGRect frame = CGRectMake(8, (self.bgView.height - image.size.height) * 0.5, image.size.width, image.size.height);
        _searchImgView = [UIImageView createWithImageView:self.bgView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

- (void)initWithSearchTextField {
    CGRect frame = CGRectMake(_searchImgView.right + 10, (self.bgView.height - 32) * 0.5, self.bgView.width - self.searchImgView.right - 10, 32);
    _searchTextField = [[UITextField alloc] initWithFrame:frame];
    _searchTextField.placeholder = @"请输入关键词搜索";
    _searchTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
//    _searchTextField.backgroundColor = [UIColor redColor];//[UIColor colorWithHexString:@"#F2F2F2"];
    _searchTextField.delegate = self;
    _searchTextField.borderStyle = UITextBorderStyleNone;
    _searchTextField.returnKeyType = UIReturnKeySearch;
    _searchTextField.keyboardType = UIKeyboardTypeDefault;
    _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchTextField.enabled = YES;
    [_bgView addSubview:_searchTextField];
}

- (void)setSearchMsg:(NSString *)searchMsg {
    _searchTextField.text = searchMsg;
}

/// 搜索
/// - Parameter sender: sender description
- (void)actionWithSearch:(id)sender {
    [self.searchTextField resignFirstResponder];
    if (self.touchSearchBlock) {
        self.touchSearchBlock(self.searchTextField.text);
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self actionWithSearch:nil];
    return true;
}

@end
